/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package apus.command.person;

import apus.command.AbstractCommand;
import apus.entity.Person;
import apus.exception.BusinessException;
import apus.persistence.PersonDAO;
import apus.services.ServiceLocator;
import java.util.Arrays;
import java.util.List;

/**
 * The <code>RemovePersonCommand</code> class represents command for removing 
 * person form the database.
 * @author  Maxim Vasilevsky
 * @author  Roman Dyatkovsky
 * @since APUS v0.4
 * @see AbstractCommand
 */
public class RemovePersonCommand extends AbstractCommand{
    private final static String PARAM_ID = "Id";
    
    private final static String[] paramsNames = { PARAM_ID };
    
    public RemovePersonCommand() {
        super("Remove person");
    }

    @Override
    public void execute() {
        String stringId = params.get(PARAM_ID);
        int ID;
        
        try {
            ID = Integer.parseInt(stringId);
        } catch (NumberFormatException e){
            throw new BusinessException("Wrong ID format: "+stringId, e);
        }
        PersonDAO personDAO = ServiceLocator.getService(PersonDAO.class);
        Person person = personDAO.read(ID);
        
        if (person==null) {
            throw new BusinessException("Person not found.");
        }
        personDAO.delete(personDAO.read(ID));
    }
    
    @Override
    public List<String> getParamNames() {
        return Arrays.asList(paramsNames);
    }
    
}
